package com.aitou.mrylive.presentation.ui.login.splash;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.aitou.mrylive.R;
import com.aitou.mrylive.presentation.ui.base.BaseActivity;
import com.aitou.mrylive.presentation.ui.login.LoginSelectActivity;
import com.aitou.mrylive.presentation.ui.main.MainActivity;

import java.util.ArrayList;

import me.relex.circleindicator.CircleIndicator;

/**
 * @author Muyangmin
 * @since 1.0.0
 * 欢迎页面
 */
public class SplashActivity extends BaseActivity implements SplashUiInterface {

    private static final int MAX_SHOW_TIME = 3000;
    private final ArrayList<Integer> mImgIds = new ArrayList<>();

    private final Runnable mRunnableOnTimeout = new Runnable() {
        @Override
        public void run() {
            startLoginSelectActivity();
        }
    };

    private SplashPresenter presenter;

//    第一次登陆哒
    private View rlFistTime;
    private View rlNormal;
    private ViewPager mViewPager;
    private CircleIndicator mPageIndicator;
    private Handler mHandler;
    private SimpleDraweeView mSimpleDraweeView;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void findViews(Bundle savedInstanceState) {
        rlFistTime = $(R.id.splash_rl_first_time);
        rlNormal = $(R.id.splash_rl_normal);
        mViewPager = $(R.id.splash_view_pager);
        mPageIndicator = $(R.id.splash_circle_indicator);
        mSimpleDraweeView = $(R.id.splash_img_normal);
    }

    @Override
    protected void init() {
        setSwipeBackEnable(false);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

        presenter = new SplashPresenter(this);
        mImgIds.add(R.drawable.img_splash_01);
//        mImgIds.add(R.drawable.img_splash_02);

        mHandler = new Handler();

        if (presenter.isFirstRun()) {
            rlFistTime.setVisibility(View.VISIBLE);
            rlNormal.setVisibility(View.GONE);
            if (mImgIds.size()<2){
                mPageIndicator.setVisibility(View.GONE);
            }
            mViewPager.setAdapter(new SplashPagerAdapter(mImgIds,
                    new SplashPagerAdapter.SplashBtnListener() {
                        @Override
                        public void onStartBtnClicked() {
                            presenter.performFirstStartNavigation(SplashActivity.this);
                        }
                    }));
            mPageIndicator.setViewPager(mViewPager);
        } else {
            rlNormal.setVisibility(View.VISIBLE);
            rlFistTime.setVisibility(View.GONE);
            mSimpleDraweeView.setImageURI(Uri.parse(("res:///" + R.drawable.img_splash_default)));
            mHandler.postDelayed(mRunnableOnTimeout, MAX_SHOW_TIME);
            presenter.autoLogin();
        }
    }

    @Override
    public void startLoginSelectActivity() {
        mHandler.removeCallbacks(mRunnableOnTimeout);
        startActivity(LoginSelectActivity.createIntent(this));
        finish();
    }

    @Override
    public void startMainActivity() {
        mHandler.removeCallbacks(mRunnableOnTimeout);
        startActivity(MainActivity.createIntent(this));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.unsubscribeTasks();
    }
}
